// Copyright 2019 The FlutterCandies author. All rights reserved.
// Use of this source code is governed by an Apache license that can be found
// in the LICENSE file.

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:photo_manager/photo_manager.dart';

/// 包装AssetEntity以提供沙箱路径的类
@immutable
class SandboxAssetEntity {
  const SandboxAssetEntity(this._originalEntity);

  final AssetEntity _originalEntity;

  /// ohos 获取沙箱路径的文件
  Future<File?> get sandboxFileOhos async {
      final String? sandboxPath = await PhotoManager.plugin.getFullFile(
        _originalEntity.id,
        isOrigin: false,
      );

      if (sandboxPath != null) {
        return File(sandboxPath);
      }

    return _originalEntity.file;
  }

  /// ohos 平台获取沙箱路径
  Future<String?> get sandboxPathOhos async {
    return PhotoManager.plugin.getFullFile(
      _originalEntity.id,
      isOrigin: false,
    );
  }

  /// 代理所有其他属性到原始AssetEntity
  String get id => _originalEntity.id;
  AssetType get type => _originalEntity.type;
  int get typeInt => _originalEntity.typeInt;
  int get width => _originalEntity.width;
  int get height => _originalEntity.height;
  int get duration => _originalEntity.duration;
  int get orientation => _originalEntity.orientation;
  bool get isFavorite => _originalEntity.isFavorite;
  String? get title => _originalEntity.title;
  int? get createDateSecond => _originalEntity.createDateSecond;
  int? get modifiedDateSecond => _originalEntity.modifiedDateSecond;
  String? get relativePath => _originalEntity.relativePath;
  double? get latitude => _originalEntity.latitude;
  double? get longitude => _originalEntity.longitude;
  String? get mimeType => _originalEntity.mimeType;
  int get subtype => _originalEntity.subtype;

  Future<File?> get file => _originalEntity.file;
  Future<File?> get originFile => _originalEntity.originFile;
  Future<Uint8List?> get originBytes => _originalEntity.originBytes;
  Future<String> get titleAsync => _originalEntity.titleAsync;
  Future<String?> get mimeTypeAsync => _originalEntity.mimeTypeAsync;

  Future<File?> loadFile({
    bool isOrigin = true,
    bool withSubtype = false,
    PMProgressHandler? progressHandler,
    PMDarwinAVFileType? darwinFileType,
  }) {
    return _originalEntity.loadFile(
      isOrigin: isOrigin,
      withSubtype: withSubtype,
      progressHandler: progressHandler,
      darwinFileType: darwinFileType,
    );
  }

  Future<Uint8List?> thumbnailDataWithSize(
    ThumbnailSize size, {
    ThumbnailFormat format = ThumbnailFormat.jpeg,
    int quality = 100,
    PMProgressHandler? progressHandler,
    int frame = 0,
  }) {
    return _originalEntity.thumbnailDataWithSize(
      size,
      format: format,
      quality: quality,
      progressHandler: progressHandler,
      frame: frame,
    );
  }

  Future<Uint8List?> thumbnailDataWithOption(
    ThumbnailOption option, {
    PMProgressHandler? progressHandler,
  }) {
    return _originalEntity.thumbnailDataWithOption(
      option,
      progressHandler: progressHandler,
    );
  }

  @override
  bool operator ==(Object other) {
    if (other is! SandboxAssetEntity) {
      return false;
    }
    return _originalEntity == other._originalEntity;
  }

  @override
  int get hashCode => _originalEntity.hashCode;

  @override
  String toString() => 'SandboxAssetEntity(${_originalEntity.toString()})';
}